//
//  StoreCategoryTableHeader.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/6.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "StoreCategoryTableHeader.h"

#import "StoreCategoryCollectionCell.h"

@interface StoreCategoryTableHeader ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionview;
@end

@implementation StoreCategoryTableHeader

+ (CGFloat)calculateHeight:(NSInteger)count {
    if (count == 0) {
        return 0;
    }
    CGFloat collectionHeight = 28 + 7.5 + 15;
    NSInteger column = count % 5 == 0 ? count / 5 : (count / 5) + 1;
    CGFloat totalHeight = (collectionHeight * column ) + (column > 0 ? ((column - 1) * 10.0) : 0);
    totalHeight += 20;
    return totalHeight;
}
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self layout];
    }
    return self;
}
- (void)layout {
    [self addSubview:self.collectionview];
    [self.collectionview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.bottom.mas_equalTo(0);
    }];
}

- (void)fillHeadWithArray:(NSMutableArray <ProClassificationModel *>*)dataArray {
    
    self.dataArray = @[].mutableCopy;
    self.dataArray = dataArray;
    [self.collectionview reloadData];
}

- (void)layoutSubviews {
    [super layoutSubviews];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    StoreCategoryCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"StoreCategoryCollectionCellID" forIndexPath:indexPath];
    cell.data2 = self.dataArray[indexPath.row];
    return cell;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    if (self.BA_StoreCategoryCellBlock) {
        self.BA_StoreCategoryCellBlock(indexPath.row);
    }
}
- (UICollectionView *)collectionview {
    if (!_collectionview) {
        CGFloat width = TTScreenWidth - 20;
        
        CGFloat cellWidth, cellHeight;
        cellWidth = width/5;
        cellHeight = 28 + 7.5 + 15;
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 10;
        flowLayout.itemSize = CGSizeMake(cellWidth, cellHeight);

        _collectionview  = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:flowLayout];
        _collectionview.delegate = self;
        _collectionview.dataSource = self;
        _collectionview.pagingEnabled = YES;
        _collectionview.showsHorizontalScrollIndicator = NO;
        _collectionview.showsVerticalScrollIndicator = NO;
        _collectionview.backgroundColor = [UIColor whiteColor];
        [_collectionview registerClass:[StoreCategoryCollectionCell class] forCellWithReuseIdentifier:@"StoreCategoryCollectionCellID"];
    }
    return _collectionview;
}

@end
